package com.byx.chatclient.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.byx.chatclient.ChatApp;
import com.byx.chatclient.R;
import com.byx.chatclient.bean.Group;
import com.byx.chatclient.entity.Constant;
import com.byx.chatclient.entity.GroupDto;
import com.byx.chatclient.entity.Result;
import com.byx.chatclient.entity.ResultCallback;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ActionRequest;
import com.byx.chatclient.utils.GradientStatusBarUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;

/**
 * @author Superb
 */
@SuppressLint("NonConstantResourceId")
public class GroupDetailActivity extends AppCompatActivity {

    @BindView(R.id.sdv_group_avatar)
    protected SimpleDraweeView mSdvGroupAvatar;
    @BindView(R.id.tv_group_name)
    protected TextView mTvGroupName;
    @BindView(R.id.tv_group_id)
    protected TextView mTvGroupId;
    @BindView(R.id.tv_group_create_date)
    protected TextView mTvCreateTime;
    @BindView(R.id.tv_group_description)
    protected TextView mTvGroupDescription;
    @BindView(R.id.btn_action)
    protected Button mBtnAction;

    protected Call<Result<GroupDto>> mGroupCall;

    private GroupDto mGroup;
    /**
     * 是否在群聊内
     */
    private boolean flag;

    private Unbinder mUnbinder;

    private static final int IN_GROUP = 200;
    private static final int NOT_IN_GROUP = 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_detail);
        GradientStatusBarUtil.getInstance().bind(this).withBackgroundResource(R.drawable.toolbar).init();
        mUnbinder = ButterKnife.bind(this);
        Intent intent = getIntent();
        Long groupId = intent.getLongExtra(Constant.GROUP_ID, 0L);
//        flag = intent.getIntExtra(Constant.INTENT_KEY_IF_IN_GROUP,200);
        mGroupCall = RetrofitFactory.getBean(ActionRequest.class).queryUserGroupDetail(ChatApp.getInstance().getUserPo().getId(),groupId);
        mGroupCall.enqueue(new ResultCallback<GroupDto>() {
            @Override
            public void onResultSuccess(GroupDto data) {
                init(data);
            }

            @Override
            public void onResultFailure(int code, String msg) {

            }
        });
    }

    private void init(GroupDto groupDto) {
        assert mGroup != null;
        mGroup = groupDto;
        // 身份不为空代表是群成员
        flag = groupDto.getRole() != null;
        mSdvGroupAvatar.setImageURI(groupDto.getAvatar());
        mTvGroupName.setText(groupDto.getName());
        mTvGroupId.setText(String.valueOf(groupDto.getId()));
        mTvCreateTime.setText(groupDto.getCreateTime());
        mTvGroupDescription.setText(TextUtils.isEmpty(groupDto.getDescription()) ? "群主很懒，暂未编辑群介绍" : groupDto.getDescription());
        mBtnAction.setText(flag ? "发消息" : "加入群聊");
    }

    @OnClick({R.id.iv_back,R.id.btn_action})
    protected void onClick(View view){
        if (view.getId() == R.id.iv_back){
            finish();
        }else if (view.getId() == R.id.btn_action){
            if (flag){
                Intent intent = new Intent(GroupDetailActivity.this, GroupChatActivity.class);
                intent.putExtra(Constant.INTENT_KEY_GROUP,mGroup);
//                intent.putExtra(Constant.INTENT_KEY_CHAT_MODE,false);
                startActivity(intent);
            }else {

            }
        }
    }

    @Override
    protected void onDestroy() {
        if (mGroupCall.isExecuted()){
            mGroupCall.cancel();
        }
        GradientStatusBarUtil.getInstance().unbind();
        mUnbinder.unbind();
        super.onDestroy();
    }
}